import {
  Body,
  Controller,
  HttpException,
  HttpStatus,
  Post,
  Req,
} from '@nestjs/common';
import { UserService } from '../../user.service';
import { Request } from 'express';
import { ApiHeader, ApiResponse, ApiTags } from '@nestjs/swagger';
import {
  ProfileItemReqDto,
  ProfileItemResFailDto,
  ProfileItemResSuccessDto,
} from 'src/user/dto/profile/item.controller.dto';
import { ValidAuthFailDto } from 'src/user/dto/middleware/auth.middleware.dto';

@ApiTags('user')
@ApiResponse({
  status: HttpStatus.FORBIDDEN,
  type: ValidAuthFailDto,
  description: '身份验证失败',
})
@Controller('user')
export class AddProfileController {
  constructor(private readonly userService: UserService) {}

  @ApiResponse({
    status: HttpStatus.CREATED,
    type: ProfileItemResSuccessDto,
    description: '创建成功',
  })
  @ApiResponse({
    status: HttpStatus.ACCEPTED,
    type: ProfileItemResFailDto,
    description: '创建失败',
  })
  @ApiHeader({
    name: 'token',
    description: '身份令牌',
  })
  @Post('profile/item')
  async addProfile(@Req() req: Request, @Body() body: ProfileItemReqDto) {
    const openid = req['user'].openid;
    const { tag, nickname, allow_stop, head_image } = body;
    await this.userService.addProfile(
      openid,
      tag,
      nickname,
      head_image,
      allow_stop,
    );
    throw new HttpException(
      {
        success: true,
        message: 'ok',
      },
      HttpStatus.CREATED,
    );
  }
}
